package com.lin.reggie.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lin.reggie.dto.SetmealDto;
import com.lin.reggie.entity.Category;
import com.lin.reggie.entity.Page;
import com.lin.reggie.entity.Setmeal;
import com.lin.reggie.entity.SetmealDish;
import com.lin.reggie.exception.BusinessException;
import com.lin.reggie.mapper.CategoryMapper;
import com.lin.reggie.mapper.DishMapper;
import com.lin.reggie.mapper.SetMealMapper;
import com.lin.reggie.mapper.SetmealDishMapper;
import com.lin.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {


    @Autowired(required = false)
    //套餐表
    private SetMealMapper setMealMapper;
    //套餐里面的菜
    @Autowired(required = false)
    private SetmealDishMapper setmealDishMapper;

    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    @Autowired(required = false)
    private DishMapper dishMapper;
    /**
     * 作用：新增套餐
     *
     * @param setmealDto
     * @return
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @Override
    @Transactional
    public void save(SetmealDto setmealDto) {
        //补全setmeal的信息，比如：修改、创建时间
        //创建时间
        setmealDto.setCreateTime(LocalDateTime.now());
        //修改时间
        setmealDto.setUpdateTime(LocalDateTime.now());

        //调用保存的方法
        setMealMapper.save(setmealDto);

        //得到套餐的菜品，给所有菜品补全信息
        List<SetmealDish>setmealDishes=setmealDto.getSetmealDishes();

        List<SetmealDish>setmealDishe = setmealDishes.stream().map((seteal->{
            seteal.setSetmealId(setmealDto.getId());
            seteal.setCreateUser(setmealDto.getCreateUser());
            seteal.setUpdateUser(setmealDto.getUpdateUser());
            seteal.setCreateTime(LocalDateTime.now());
            seteal.setUpdateTime(LocalDateTime.now());
            seteal.setSort(0);
            return seteal;
        })).collect(Collectors.toList());

        //批量插入套餐菜品表菜品
        setmealDishMapper.saveBatch(setmealDishe);

    }

    @Override
    public Page<SetmealDto> findByPage(Integer page, Integer pageSize, String name) {
        //设置当前页面大小
        PageHelper.startPage(page,pageSize);
        //2.根据name查询数据，得到list集合，List<Setmeal> select * from setmeal
        List<Setmeal> setmealList = setMealMapper.findByName(name);
        //3.创建PageInfo对象，只有创建PageInfo对象了，上面的sql语句才会真正的去执行
        PageInfo<Setmeal> pageInfo = new PageInfo<>(setmealList);
        //4.遍历List<Setmeal>,把Setmeal转换为SetmealDto
        ArrayList<SetmealDto> setmealDtoList = new ArrayList<>();
        for (Setmeal setmeal : pageInfo.getList()) {
            SetmealDto setmealDto1 = new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto1);
            Category category=categoryMapper.findById(setmeal.getCategoryId());
            setmealDto1.setCategoryName(category.getName());
            setmealDtoList.add(setmealDto1);
        }
        Page<SetmealDto> pageResult = new Page<>(setmealDtoList,pageInfo.getTotal(),page,pageSize);
        return pageResult;
    }
    @CacheEvict(value = "setmealCache",allEntries = true)
    @Transactional
    @Override
    public boolean deleteByIds(List<Long>ids,Long updateUser){
        //判断传递过来的套餐  id  列表查询  是否存在  已启用的套餐
        //查询已启动套餐的个数，个数大于0代表存在，否则不存在
        Integer count = setMealMapper.countByIds(ids);
        //2.如果存在，抛出异常"该套餐"正在启用状态，不能删除"
        if (count>0){
            throw new BusinessException("该套餐正在启售状态,暂时不能删除");
        }
        //3.调用数据访问层进行批量删除，传递修改人，最新的修改时间
        //批量删除套餐和菜品关系的数据
        setmealDishMapper.deleteBySetmealIds(ids,updateUser,LocalDateTime.now());
        //批量删除套餐
        setMealMapper.deleteByIds(ids,updateUser,LocalDateTime.now());
        //返回数据
        return true;
    }

    //查询套餐写入缓存
    @Cacheable(value = "setmealCache",key = "#categoryId+'_'+#status",unless = "#result==null")
    @Override
    public List<Setmeal> findSetmeal(Long categoryId ,Integer status) {
        List<Setmeal> setmeal = setMealMapper.findSetmeal(categoryId,status);
        return setmeal;
    }
}
